//
//  SEPatientInfoView.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/17.
//

import UIKit

@IBDesignable class SEPatientInfoView: UIView {
    
    var editClosure: (() -> Void)?

    var view: UIView!
    
    @IBOutlet private weak var backgroundImageView: UIImageView!
    @IBOutlet private weak var oneselfLabel: UILabel! {
        didSet {
            oneselfLabel.layer.cornerRadius = 2
            oneselfLabel.layer.masksToBounds = true
        }
    }
    @IBOutlet private weak var nameLabel: UILabel!
    @IBOutlet private weak var idCardLabel: UILabel!
    @IBOutlet private weak var sexLabel: UILabel!
    @IBOutlet private weak var ageLabel: UILabel!
    @IBOutlet private weak var editButon: UIButton!
    
    @IBOutlet private var oneselfLabelLeftConstraint: NSLayoutConstraint!
    @IBOutlet private var nameLabelLeftConstraint: NSLayoutConstraint!
    
    @IBInspectable var hideEditButton: Bool {
        get { editButon.isHidden }
        set { editButon.isHidden = newValue }
    }
    
    @IBInspectable var hideOneselfLabel: Bool {
        get { oneselfLabel.isHidden }
        set {
            oneselfLabel.isHidden = newValue
            oneselfLabelLeftConstraint.priority = newValue ? .defaultLow : .defaultHigh
            nameLabelLeftConstraint.priority = newValue ? .defaultHigh : .defaultLow
        }
    }
    
    @IBAction private func editAction() {
        editClosure?()
    }
    
    // MARK: - life cycle
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }

    func updateView(_ viewModel: SEPatientDetailViewModel) {
        view.layer.borderColor = viewModel.borderColor
        nameLabel.text = viewModel.model.name
        ageLabel.text = viewModel.model.age
        sexLabel.text = viewModel.sexStr
        backgroundImageView.image = viewModel.backgroundImage
        oneselfLabel.backgroundColor = viewModel.onselfBackgroundColor
        hideOneselfLabel = viewModel.relationship != .myself
        idCardLabel.text = viewModel.idNumberStr
    }
    
    private func setupUI() {
        backgroundColor = .clear
        
        view = loadViewFromNib()
        
        view.setLayerCorners(rect: bounds, radius: 14, corners: [.topLeft, .topRight])
        view.layer.borderWidth = 1
        view.layer.borderColor = Asset.background.color.cgColor
        view.layer.masksToBounds = true
    }
    
    // MARK: - load nib for view
    
    private func loadViewFromNib() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName(), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        view.frame = bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(view)

        return view
    }

    private func nibName() -> String {
        return String(describing: type(of: self))
    }
}
